<script setup lang="ts">
import { computed } from 'vue';
import { $t } from '@/locales';
import { useFormRules, useNaiveForm } from '@/hooks/common/form';

defineOptions({
  name: 'UserSearch'
});

interface Emits {
  (e: 'reset'): void;
  (e: 'search'): void;
}

const emit = defineEmits<Emits>();

const options = [
  {
    label: '特色烤串',
    value: '特色烤串'
  },
  {
    label: '把把系列',
    value: '把把系列'
  },
  {
    label: '烤串系列',
    value: '烤串系列'
  },
  {
    label: '酒水饮料',
    value: '酒水饮料'
  },
  {
    label: '特色菜系列',
    value: '特色菜系列'
  },
  {
    label: '江湖菜系列',
    value: '江湖菜系列'
  },
  {
    label: '基础消费',
    value: '基础消费'
  }
];

const { formRef, validate, restoreValidation } = useNaiveForm();

const model = defineModel<Api.Food.DishSearchParams>('model', { required: true });

type RuleKey = Extract<keyof Api.Food.DishSearchParams, 'name' | 'category'>;

const rules = computed<Record<RuleKey, App.Global.FormRule>>(() => {
  const { patternRules } = useFormRules(); // inside computed to make locale reactive

  return {};
});

async function reset() {
  await restoreValidation();
  emit('reset');
}

async function search() {
  await validate();
  emit('search');
}
</script>

<template>
  <NCard :title="$t('common.search')" :bordered="false" size="small" class="card-wrapper">
    <NForm ref="formRef" :model="model" :rules="rules" label-placement="left" :label-width="80">
      <NGrid responsive="screen" item-responsive>
        <NFormItemGi span="24 s:12 m:6" label="菜品名称" path="name" class="pr-24px">
          <NInput v-model:value="model.name" placeholder="菜名" />
        </NFormItemGi>
        <NFormItemGi span="24 s:12 m:6" label="菜品分类" path="category" class="pr-24px">
          <NSelect v-model:value="model.category" placeholder="菜品分量" :options="options" clearable />
        </NFormItemGi>
        <NFormItemGi span="24 s:12 m:6" label="价格" path="price" class="pr-24px">
          <NInputNumber v-model:value="model.price" placeholder="价格" />
        </NFormItemGi>
        <NFormItemGi span="24 s:12 m:6" label="折扣" path="sale" class="pr-24px">
          <NInputNumber v-model:value="model.sale" placeholder="折扣" />
        </NFormItemGi>
        <NFormItemGi span="24 m:12" class="pr-24px">
          <NSpace class="w-full" justify="end">
            <NButton @click="reset">
              <template #icon>
                <icon-ic-round-refresh class="text-icon" />
              </template>
              {{ $t('common.reset') }}
            </NButton>
            <NButton type="primary" ghost @click="search">
              <template #icon>
                <icon-ic-round-search class="text-icon" />
              </template>
              {{ $t('common.search') }}
            </NButton>
          </NSpace>
        </NFormItemGi>
      </NGrid>
    </NForm>
  </NCard>
</template>

<style scoped></style>
